﻿using FasterWPF;
using FasterWPF.Dialogs;
using FasterWPFDemo.UserControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace FasterWPFDemo
{
    public static class ListBoxItemGuids
    {
        public static string ItemA = "A4525F82-6F1A-5973-B5F8-251635D4564F";
        public static string ItemB = "967AB79E-81E2-790C-8E84-DF01643C6D77";
    }

    public class TabItem09 : UserControl, IUserControlGuid
    {
        private string _userCtlGuid = null;

        public string UserControlGuid
        {
            get
            {
                return _userCtlGuid;
            }

            set
            {
                _userCtlGuid = value;
            }
        }

        ScrollViewer scvw = null;

        public TabItem09()
        {
            //InitializeComponent(); XAML removed. Initialize via code-behind.

            this.Initialize(ContainerType.VerticalPanel);

            // this.Width = 850D;
            // this.Height = 750D;

            this.BeginSettings<UserControl>()
                .SetSelectorClass<UserControl>(0, 0, "tabLabel")
                .Set<Label, UserControl>(0, 4, "Width", 159D)
                .SetItemBorderColorAndThickness<UserControl>(Brushes.Gray, new Thickness(3)).EndSettings();
        }
         
        public void LoadUserControl()
        {
            //1. Initialization and Settings for ScrollViewer
            string scguid1 = Guid.NewGuid().ToString();
            string scguid2 = Guid.NewGuid().ToString();

            scvw = new ScrollViewer();

            scvw.Initialize(400D, 305.0D, ContainerType.Grid, new Thickness(10, 10, 10, 100), 4, 3);
            scvw.SetCompositeGridDimensions<ScrollViewer>(385D, 455.0D, 1, 1, 400D, GridUnitType.Pixel, 430D, GridUnitType.Pixel);
            scvw.SetBackgroundColor(Brushes.WhiteSmoke);
            scvw.BeginSettings<ScrollViewer>()                ////VERTICAL SCROLLBAR
                .SetVerticalScrollBarColor(Brushes.WhiteSmoke)
                .SetVerticalScrollUpButtonColors("WhiteSmoke", "LawnGreen", "Green", "Green", "Green", "0", "Gray", new Thickness(1), "Black", "LawnGreen", "M 0 4 L 7 4 L 3.5 0 Z")
                .SetVerticalPageUpButtonColors("#DDDDDD", "Gray", "Gray", "#DDDDDD") //, "2", "White", new Thickness(3, 3, 3, 3))
                .SetVerticalPageDownButtonColors("#DDDDDD", "#DDDDDD", "Gray", "Gray") //,"5", "Gray", new Thickness(3, 3, 3, 3))
                .SetVerticalScrollDownButtonColors("WhiteSmoke", "LawnGreen", "Green", "Green", "Green", "0", "Gray", new Thickness(1), "Black", "LawnGreen", "M 0 0 L 3.5 4 L 7 0 Z")
                .SetVerticalScrollThumbColors("ForestGreen", "LawnGreen", "LawnGreen", "#DDDDDD", "1", "1")

                //HORIZONTAL SCROLLBAR
                .SetHorizontalScrollBarColor(Brushes.WhiteSmoke)
                .SetHorizontalScrollRightButtonColors("#BBB", "Blue", "Red", "Red", "Red", "0", "#FFEE", new Thickness(1), "Black", "Orange", "M 0 0 L 4 4 L 0 8 Z")
                .SetHorizontalPageRightButtonColors("#DDDDDD", "#DDDDDD", "Gray", "Gray") //, "#FFEE", "0", new Thickness(0, 0, 0, 0))
                .SetHorizontalPageLeftButtonColors("#DDDDDD", "Gray", "Gray", "#DDDDDD") // , "#FFEE", "0", new Thickness(0, 0, 0, 0))
                .SetHorizontalScrollLeftButtonColors("#BBB", "Blue", "Red", "Red", "Red", "0", "#FFEE", new Thickness(1), "Black", "Orange", "M 4 0 L 4 8 L 0 4 Z")
                .SetHorizontalScrollThumbColors("#0088FF", "#0088FF", "Gray", "#DDDDDD", "1", "1")

                .SetScrollViewerCornerRectangleColor(Brushes.White)

                .EndSettings<ScrollViewer>();


            //2. Initialization and Settings for GroupBox
            string gxguid1 = null;
            gxguid1 = Guid.NewGuid().ToString();
            string cxanvGuid = Guid.NewGuid().ToString();
            string cxanvGuid2 = Guid.NewGuid().ToString();
            GroupBox gpb = new GroupBox();
            gpb.Initialize(349D, 405.0D, ContainerType.ContentControlPanel, 4, 3);
            TextBlock tlb1 = new TextBlock(new Run("Vacation Destinations"));
            tlb1.Background = Brushes.WhiteSmoke;
            FontSettings fs1 = FontExt.CreateFont("Arial", 15D, FontWeights.Bold, FontStyles.Normal);
            FontExt.ApplyFontSettings(fs1, tlb1);
            gpb.Header = tlb1;
            gpb.BorderBrush = Brushes.Green;
            gpb.BorderThickness = new Thickness(5);
            gpb.Padding = new Thickness(7,5, 5, 7);

            gpb.ApplyGroupBoxStrategyToRemoveExtraWhiteBorders(10);

            double counter = 3D;
            bool switchTransition = false;
            gpb.BeginSettings<GroupBox>()
                .SetItemBorderSettings(double.NaN, 400D, Brushes.Silver, new Thickness(0))
                .SubscribeDependencyEventOnParent("IsEnabledChanged", (ve =>
                {
                    //mimmick something like the Visual State Manager
                    if (gpb.IsEnabled == false)
                    {
                        //1. DISABLED STATE
                        if ((counter - 0.3D) > 0.5D)
                        {
                            //make animation slightly faster each time down to half a second . . .
                            counter = counter - 0.3D;
                        }
                        Canvas myCanvas = gpb.GetChildFromParent<Canvas, GroupBox>(gxguid1, 0, 0);
                        
                        //unsubscribe any events if necessary
                        // myCanvas.UnsubscribeContainerEventByKey<Canvas>("GotFocus", cxanvGuid);
                        
                        Border oldComposite = myCanvas.GetCompositeFromParent(cxanvGuid);

                        string transferData1_1 = oldComposite.GetChildFromComposite<TextBox, Canvas>(1, 1).Text;
                        string transferData2_1 = oldComposite.GetChildFromComposite<TextBox, Canvas>(2, 1).Text;                 

                        myCanvas.SetBackgroundColor(Brushes.Silver);

                        //optionally change or add new settings on-the-fly . . .
                        //if(!myCanvas.ContainsFontSettingAtXY(2, 1))
                        //{
                        //     myCanvas.AddSetting<TextBlock, Canvas>(2, 1,"FontFamily", new FontFamily("Arial"));
                        //     myCanvas.AddSetting<TextBlock, Canvas>(2, 1,"FontSize", 11D);
                        //     myCanvas.AddSetting<TextBlock, Canvas>(2, 1,"FontWeight",  FontWeights.Bold);
                        //     myCanvas.AddSetting<TextBlock, Canvas>(2, 1,"FontStyle", FontStyles.Normal);
                        //}

                        //create the new composite
                        //transfer data so as to maintain data from prior composite
                        Border mynewComposite = myCanvas.BeginComposite(cxanvGuid2)
                          .AddText<Canvas>(0, 1, "This is a brand new Composite!")
                          .AddText<Canvas>(1, 0, "Attraction:")
                          .AddText<Canvas>(2, 0, "Location:")
                          .AddText<Canvas>(1, 1, transferData1_1)
                          .AddText<Canvas>(2, 1, transferData2_1)
                          .EndComposite<Canvas, CanvasArgs>(new CanvasArgs(0, 0, 0));

                        mynewComposite.GetChildFromComposite<TextBlock, Canvas>(0, 1).SetValue(Grid.ColumnSpanProperty, 2); 
                        mynewComposite.GetChildFromComposite<TextBlock, Canvas>(1, 1).TextAlignment= TextAlignment.Left;
                        mynewComposite.GetChildFromComposite<TextBlock, Canvas>(2, 1).TextAlignment = TextAlignment.Left;
                        mynewComposite.GetChildFromComposite<TextBlock, Canvas>(1, 1).VerticalAlignment = VerticalAlignment.Center;
                        mynewComposite.GetChildFromComposite<TextBlock, Canvas>(2, 1).VerticalAlignment = VerticalAlignment.Center;

                        if (switchTransition)
                        {
                            oldComposite.ScaleOutScaleIn(mynewComposite, AnimationExt.ScaleType.RightToLeft, counter, () =>
                           {
                               if (myCanvas.IsCompositeOnParent(cxanvGuid))
                               {
                                   myCanvas.RemoveByKey(cxanvGuid);
                                   myCanvas.RemoveZIndexSetting<Border, Canvas>(0, 0);
                                   myCanvas.SetZIndex<Border, Canvas>(0, 0, 1); //re-set z-index on new visible composite

                                   //re-enable flatbutton after animation has fully completed
                                   this.GetCompositeFromParent(_userCtlGuid).GetChildFromComposite<Border, UserControl>(0, 3).IsEnabled = true;
                                   this.GetCompositeFromParent(_userCtlGuid).GetChildFromComposite<Border, UserControl>(0, 2).IsEnabled = true;
                           
                               }
                           }, oldComposite.GetParentFromComposite<Canvas>().GetContainerFromComposite<Grid, Canvas>(oldComposite), Colors.Gray, counter);
                        }
                        else
                        {
                            oldComposite.FadeOutFadeIn(mynewComposite, counter, () =>
                            {
                                if (myCanvas.IsCompositeOnParent(cxanvGuid))
                                {
                                    myCanvas.RemoveByKey(cxanvGuid);
                                    myCanvas.RemoveZIndexSetting<Border, Canvas>(0, 0);
                                    myCanvas.SetZIndex<Border, Canvas>(0, 0, 1); //re-set z-index on new visible composite

                                    //re-enable flatbutton after animation has fully completed
                                    this.GetCompositeFromParent(_userCtlGuid).GetChildFromComposite<Border, UserControl>(0, 3).IsEnabled = true;
                                    this.GetCompositeFromParent(_userCtlGuid).GetChildFromComposite<Border, UserControl>(0, 2).IsEnabled = true;
                           
                                }
                            });                        
                        }
                    }
                    else
                    {
                        //2. ENABLED STATE
                        if ((counter - 0.3D) > 0.5D)
                        {
                            //make animation slightly faster each time down to half a second . . .
                            counter = counter - 0.3D;
                        }

                        Canvas myCanvas = gpb.GetChildFromParent<Canvas, GroupBox>(gxguid1, 0, 0);

                        Border oldComposite = myCanvas.GetCompositeFromParent(cxanvGuid2);

                        string transferData1_1 = oldComposite.GetChildFromComposite<TextBlock, Canvas>(1, 1).GetText();
                        string transferData2_1 = oldComposite.GetChildFromComposite<TextBlock, Canvas>(2, 1).GetText();

                        myCanvas.SetBackgroundColor(Brushes.WhiteSmoke); //restore original color

                        Border mynewComposite = myCanvas.BeginComposite<Canvas>(cxanvGuid)
                              .AddAnything<TextBox, Canvas>(1, 1)
                              .AddAnything<TextBox, Canvas>(2, 1)
                              .AddText<Canvas>(1, 0, "Attraction:")
                              .AddText<Canvas>(2, 0, "Location:")
                              .AddText<Canvas>(3, 0, "this is text at row 3 column 0")
                              .AddText<Canvas>(0, 0, "Arboretum")
                              .AddImage<Canvas>(0, 1, @"pack://siteoforigin:,,,/Images/woods.bmp", UriKind.Absolute, Double.NaN, 1, 1)
                              //.SubscribeEventOnContainer<Grid, Canvas>("GotFocus", new RoutedEventHandler(
                          //     (sender, e3) => { MessageBox.Show("GotFocus " + e3.Source.ToString()); }))
                              .SetColorOnContainer<Grid, Canvas>(Brushes.Transparent)
                              .SetMouseOverColorOnContainer<Grid, Canvas>(Brushes.LightYellow)                              
                              .EndComposite<Canvas, CanvasArgs>(new CanvasArgs(0, 0, 0));

                        mynewComposite.GetChildFromComposite<TextBox, Canvas>(1, 1).Text = transferData1_1;
                        mynewComposite.GetChildFromComposite<TextBox, Canvas>(2, 1).Text = transferData2_1;
                        if (switchTransition)
                        {
                            oldComposite.ScaleOutScaleIn(mynewComposite, AnimationExt.ScaleType.BottomToTop, counter, () =>
                           {
                               if (myCanvas.IsCompositeOnParent(cxanvGuid2))
                               {
                                   myCanvas.RemoveByKey(cxanvGuid2);
                                   myCanvas.RemoveZIndexSetting<Border, Canvas>(0, 0);
                                   myCanvas.SetZIndex<Border, Canvas>(0, 0, 1); //re-set z-index on new visible composite

                                   //re-enable flatbutton after animation has fully completed
                                   this.GetCompositeFromParent(_userCtlGuid).GetChildFromComposite<Border, UserControl>(0, 3).IsEnabled = true;
                                   this.GetCompositeFromParent(_userCtlGuid).GetChildFromComposite<Border, UserControl>(0, 2).IsEnabled = true;
                               }
                           }, oldComposite.GetParentFromComposite<Canvas>().GetContainerFromComposite<Grid, Canvas>(oldComposite), Colors.Gray, counter);
                        }
                        else
                        {
                            oldComposite.FadeOutFadeIn(mynewComposite, counter, () =>
                            {
                                if (myCanvas.IsCompositeOnParent(cxanvGuid2))
                                {
                                    myCanvas.RemoveByKey(cxanvGuid2);
                                    myCanvas.RemoveZIndexSetting<Border, Canvas>(0, 0);
                                    myCanvas.SetZIndex<Border, Canvas>(0, 0, 1); //re-set z-index on new visible composite

                                    //re-enable flatbutton after animation has fully completed
                                    this.GetCompositeFromParent(_userCtlGuid).GetChildFromComposite<Border, UserControl>(0, 3).IsEnabled = true;
                                    this.GetCompositeFromParent(_userCtlGuid).GetChildFromComposite<Border, UserControl>(0, 2).IsEnabled = true;                           
                                }
                            });
                        }
                        
                       myCanvas.SubscribeEventOnChild<TextBox, Canvas, TextChangedEventArgs>(cxanvGuid, 2, 1, "TextChanged",(
                               ve2 => { MessageBox.Show("Text Changed Added Length: " + ve2.EventArgs.Changes.ToList().FirstOrDefault().AddedLength.ToString()); }));
                    }

                }))
                .EndSettings<GroupBox>();
            

            //3. Build the BeginComposite Chains to Nest Items 

            Border thisUserControl = null;

            thisUserControl = this.BeginComposite<UserControl>(_userCtlGuid)
                  .AddLabel<UserControl>(0, 0, "Tab Item 9 -  Group Box and Transitions Example", Brushes.LawnGreen)

                  .AddExisting<ScrollViewer, UserControl>(0, 1,
                        scvw.BeginComposite<ScrollViewer>(scguid2)
                        .AddExisting<GroupBox, ScrollViewer>(0, 0,
                                 gpb.BeginComposite<GroupBox>(gxguid1)
                                 .AddAnyIParent<Canvas, GroupBox>(0, 0,
                                 canv =>
                                 {
                                     canv.Initialize(ContainerType.Grid);

                                     canv.Height = gpb.Height - 45D;
                                     canv.SetCompositeGridDimensions<Canvas>(Double.NaN, canv.Height, 5, 3, 107D, GridUnitType.Pixel, 43D, GridUnitType.Pixel);
                                     canv.SetBackgroundColor(Brushes.WhiteSmoke);
                                     canv.BeginSettings()
                                         .SetItemBorderSettings(double.NaN, double.NaN, Brushes.Silver, new Thickness(1))
                                         .SetUseLayoutRounding<Canvas>(true)
                                         .SetFont(0, 0, "Arial", 11, FontWeights.Bold, FontStyles.Normal)
                                         .SetFont(1, 0, "Arial", 11, FontWeights.Bold, FontStyles.Normal)

                                        // .SetTextBackground(0, 0, Brushes.LawnGreen)
                                         .SetTextEffect<Canvas>(0, 0, new System.Windows.Media.Effects.DropShadowEffect())
                                         .SetHorizontalAlignmentForText<Canvas>(1, 1, TextAlignment.Center)
                                         .SetHorizontalAlignmentForText<Canvas>(2, 1, TextAlignment.Center)

                                         .SetAttachedProperty<TextBlock, Canvas>("ColumnSpan", 3, 0, txtBlock => { txtBlock.SetValue(Grid.ColumnSpanProperty, 2); })
                                         .Set<CheckBox, Canvas>("Content", "Hello World")
                                         .Set<TextBox, Canvas>("MaxHeight", 30D)
                                         .Set<TextBox, Canvas>(1, 1, "Text", "Arboretum")
                                         .Set<TextBox, Canvas>(2, 1, "Text", "Lisle, Illinois")
                                         .Set<ListBox, Canvas>("Margin", new Thickness(1, 1, 1, 1))
                                         .SetVerticalAlignment<Canvas>(2, 1, VerticalAlignment.Bottom)
                                         .SetVerticalAlignment<Canvas>(0, 1, VerticalAlignment.Center)
                                         .SetVerticalAlignment<Canvas>(0, 0, VerticalAlignment.Center)
                                         .SetHorizontalAlignmentForText<Canvas>(0, 0, TextAlignment.Center)
                                         .SetHorizontalAlignmentForText<Canvas>(1, 0, TextAlignment.Right)
                                         .SetVerticalAlignment<Canvas>(1, 0, VerticalAlignment.Center)
                                         .SetTextPadding<Canvas>(1, 0, new Thickness(7))
                                         .SetHorizontalAlignmentForText<Canvas>(2, 0, TextAlignment.Right)
                                         .SetVerticalAlignment<Canvas>(2, 0, VerticalAlignment.Center)
                                         .SetTextPadding<Canvas>(2, 0, new Thickness(7))
                                         .SetFont(2, 0, "Arial", 11, FontWeights.Bold, FontStyles.Normal)
                                         .SetItemBorderColorAndThickness(Brushes.Blue, new Thickness(0))
                                         .EndSettings();

                                     canv.BeginComposite<Canvas>(cxanvGuid)
                                      .AddAnything<TextBox, Canvas>(1, 1)
                                      .AddAnything<TextBox, Canvas>(2, 1)
                                      .SubscribeEventOnChild<TextBox, Canvas, TextChangedEventArgs>(2, 1, "TextChanged", (
                                 (sender, e) => { MessageBox.Show("Text Changed Added Length: " + e.Changes.ToList().FirstOrDefault().AddedLength.ToString()); }))
                                      .AddText<Canvas>(1, 0, "Attraction:")
                                      .AddText<Canvas>(2, 0, "Location:")
                                      .AddText<Canvas>(3, 0, "this is text at row 3 column 0")
                                      .AddText<Canvas>(0, 0, "Arboretum")
                                      .AddImage<Canvas>(0, 1, @"pack://siteoforigin:,,,/Images/woods.bmp", UriKind.Absolute, Double.NaN, 1, 1).SubscribeEventOnContainer<Grid, Canvas, RoutedEventArgs>("GotFocus", (
                                 (sender, e) => { MessageBox.Show("GotFocus " + e.Source.ToString()); }))
                                      .SetColorOnContainer<Grid, Canvas>(Brushes.Transparent) // necessary for hit-testing for mouseovercolor
                                      .SetMouseOverColorOnContainer<Grid, Canvas>(Brushes.LightYellow)
                                      .EndComposite<Canvas, CanvasArgs>(new CanvasArgs(0, 0, 1));
                                 })
                                .EndComposite<GroupBox, GroupBoxArgs>(null)
                                .GetParentFromComposite<GroupBox>())
                //.AddText<ScrollViewer>(0,1,"Test")
                        .EndComposite<ScrollViewer, ScrollViewerArgs>(null)
                        .GetParentFromComposite<ScrollViewer>())
                  .AddFlatButton<UserControl>(0, 2, 175D, 87D,
                  "Click here to\nchange toggle state\nvia Scale Transition.", Brushes.GhostWhite, new Thickness(2), Brushes.LightSalmon, new CornerRadius(3), new Thickness(2), new Thickness(100, 0, 0, 0), Brushes.LightSlateGray, Brushes.White,
                  (o, ev) =>
                  {
                      switchTransition = true;
                      //disable flatbutton temporarily to allow animation to fully complete
                      this.GetCompositeFromParent(_userCtlGuid).GetChildFromComposite<Border, UserControl>(0, 3).IsEnabled = false;
                      this.GetCompositeFromParent(_userCtlGuid).GetChildFromComposite<Border, UserControl>(0, 2).IsEnabled = false;
                      gpb.IsEnabled = !gpb.IsEnabled;
                  })
                  .AddFlatButton<UserControl>(0, 3, 175D, 87D,
                  "Click here to\nchange toggle state\nvia Fade Transition.", Brushes.GhostWhite, new Thickness(2), Brushes.LightGreen, new CornerRadius(3), new Thickness(2), new Thickness(100, 0, 0, 0), Brushes.LightSlateGray, Brushes.White,
                  (o, ev) =>
                  {
                      switchTransition = false;
                      //disable flatbutton temporarily to allow animation to fully complete
                      this.GetCompositeFromParent(_userCtlGuid).GetChildFromComposite<Border, UserControl>(0, 3).IsEnabled = false;
                      this.GetCompositeFromParent(_userCtlGuid).GetChildFromComposite<Border, UserControl>(0, 2).IsEnabled = false;
                      gpb.IsEnabled = !gpb.IsEnabled;
                  })
                  .AddLabel<UserControl>(0, 4, "Cascading ListBox Example", Brushes.Khaki)
                  .AddAnyIParent<ListBox, UserControl>(0, 5, myIParent =>
                  {
                       myIParent.Initialize(100D, 100D, ContainerType.VerticalPanel, null, null);
                       myIParent.BeginSettings()
                           .SetFontOnLabel(0, 0, "Arial", 12, FontWeights.DemiBold, FontStyles.Italic)
                           .SetSelectedContentControlColor(Colors.Green)
                           .EndSettings();

                       //Must subscribe Selection Changed On Parent BEFORE adding Child Composites
                       myIParent.SubscribeSelectionChangedOnParent((o, e) =>
                       {
                           if (e.SelectedKey() == ListBoxItemGuids.ItemA) //A
                           {
                               ListBox theOtherListBox = thisUserControl.GetChildFromComposite<ListBox, UserControl>(0, 6);
                               //get the other ListBox, then the First Child in the other ListBox and check its Text
                               if (theOtherListBox.Items.Count > 0 &&
                                   (theOtherListBox.GetChildFromParent<TextBlock, ListBox>(
                                   (thisUserControl.GetChildFromComposite<ListBox, UserControl>(0, 6).GetCompositeLookupDictionary().Keys.First<string>())
                                   , 0, 0) as TextBlock).GetText() == "List Box Item A-1")
                               {
                                   //Both are A, so do nothing
                               }
                               else
                               {
                                   //Load A
                                   theOtherListBox.RemoveAll();

                                   theOtherListBox.BeginComposite()
                                       .AddText(0, 0, "List Box Item A-1")
                                       .EndComposite<ListBox, ListBoxArgs>(null);

                                   theOtherListBox.BeginComposite()
                                       .AddText(0, 0, "List Box Item A-2")
                                       .EndComposite<ListBox, ListBoxArgs>(null);

                                   theOtherListBox.BeginComposite()
                                       .AddText(0, 0, "List Box Item A-3")
                                       .EndComposite<ListBox, ListBoxArgs>(null);
                               }
                           }
                           else  //B
                           {
                               ListBox theOtherListBox = thisUserControl.GetChildFromComposite<ListBox, UserControl>(0, 6);
                               //get the other ListBox, then the First Child in the other ListBox and check its Text
                               if (theOtherListBox.Items.Count > 0 &&
                                   (theOtherListBox.GetChildFromParent<TextBlock, ListBox>(
                                   (thisUserControl.GetChildFromComposite<ListBox, UserControl>(0, 6).GetCompositeLookupDictionary().Keys.First<string>())
                                   , 0, 0) as TextBlock).GetText() == "List Box Item B-1")
                               {
                                   //Both are B, so do nothing
                               }
                               else
                               {
                                   //Load B
                                   theOtherListBox.RemoveAll();

                                   theOtherListBox.BeginComposite()
                                       .AddText(0, 0, "List Box Item B-1")
                                       .EndComposite<ListBox, ListBoxArgs>(null);

                                   theOtherListBox.BeginComposite()
                                       .AddText(0, 0, "List Box Item B-2")
                                       .EndComposite<ListBox, ListBoxArgs>(null);

                                   theOtherListBox.BeginComposite()
                                       .AddText(0, 0, "List Box Item B-3")
                                       .EndComposite<ListBox, ListBoxArgs>(null);
                               }
                           }
                       });

                       myIParent.BeginComposite(ListBoxItemGuids.ItemA)
                           .AddText(0, 0, "List Box Item A")
                           .EndComposite<ListBox, ListBoxArgs>(null);

                       myIParent.BeginComposite(ListBoxItemGuids.ItemB)
                           .AddText(0, 0, "List Box Item B")
                           .EndComposite<ListBox, ListBoxArgs>(null);

                       myIParent.BeginComposite()
                    .AddText(0, 0, "List Box Item C")
                    .EndComposite<ListBox, ListBoxArgs>(null);

                       myIParent.BeginComposite()
                    .AddText(0, 0, "List Box Item D")
                    .EndComposite<ListBox, ListBoxArgs>(null);

                       myIParent.BeginComposite()
                    .AddText(0, 0, "List Box Item E")
                    .EndComposite<ListBox, ListBoxArgs>(null);

                       myIParent.BeginComposite()
                    .AddText(0, 0, "List Box Item F")
                    .EndComposite<ListBox, ListBoxArgs>(null);

                       myIParent.BeginComposite()
                    .AddText(0, 0, "List Box Item G")
                    .EndComposite<ListBox, ListBoxArgs>(null);

                       myIParent.BeginComposite()
                    .AddText(0, 0, "List Box Item H")
                    .EndComposite<ListBox, ListBoxArgs>(null);

                       myIParent.BeginComposite()
                    .AddText(0, 0, "List Box Item I")
                    .EndComposite<ListBox, ListBoxArgs>(null);

                       myIParent.BeginComposite()
                    .AddText(0, 0, "List Box Item J")
                    .EndComposite<ListBox, ListBoxArgs>(null);   
                   })
                  .AddAnyIParent<ListBox, UserControl>(0, 6, myIParent =>
                    {
                        myIParent.Initialize(100D, 100D, ContainerType.VerticalPanel, null, null);
                        myIParent.BeginSettings()
                            .SetFontOnLabel(0, 0, "Arial", 12, FontWeights.DemiBold, FontStyles.Italic)
                            .SetSelectedContentControlColor(Colors.LawnGreen)
                            .EndSettings();

                        myIParent.SubscribeSelectionChangedOnParent((o, e) =>
                        {
                            Canvas canVas = (Canvas)Application.Current.Properties["Canvas"];
                            string dialogGuid = Guid.NewGuid().ToString();

                            if (e.SelectedKey() != String.Empty)
                            {
                                DialogHelpers.ShowOKDialog(canVas, dialogGuid, "  Clicked '" + myIParent.GetChildFromParent<TextBlock, ListBox>(e.SelectedKey(), 0, 0).GetText() + "'.", 200D, 505D, 405D, Brushes.Black, new Thickness(1), Brushes.WhiteSmoke, Brushes.White, Brushes.Gray, Brushes.Black, Brushes.LightGray, false);
                            }
                        });
                    })
                  .EndComposite<UserControl, UserControlArgs>(null);


          //  ListBox theListBox = thisUserControl.GetChildFromComposite<ListBox, UserControl>(0, 4);

           

           //Test unsubscribing
            Canvas myCanvas3 = gpb.GetChildFromParent<Canvas, GroupBox>(gxguid1, 0, 0);
            myCanvas3.UnsubscribeContainerEventByKey<Canvas>("GotFocus", cxanvGuid);
        }

    }
}
